﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Spring.Web.Mvc;
using System.Web.Security;
using System.Text;
using Spring.Context.Support;
using Spring.Context;
using Service.Interface;
using Domain;


namespace Clannan
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : SpringMvcApplication
    {
        private static log4net.ILog logger = log4net.LogManager.GetLogger("ReflectionLayout");

        protected override void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "NoteBook", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

        }

        //protected void Application_Start()
        //{
        //    AreaRegistration.RegisterAllAreas();

        //    RegisterRoutes(RouteTable.Routes);
        //}

        protected override void Application_Start(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();

            base.Application_Start(sender, e);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            //if (this.Server.GetLastError() == null)
            //{
            //    return;
            //}
            
            //Exception ex = this.Server.GetLastError().GetBaseException();
            //LogMessage log = new LogMessage();
            //log.Message = ex.Message;
            //log.Source = ex.Source;
            //if (Request.Form != null)
            //{
            //    log.Form = this.Request.Form.ToString();
            //}
            //if (Request.QueryString != null)
            //{
            //    log.QueryString = this.Request.QueryString.ToString();
            //}
            //log.TargetSite = ex.TargetSite.ToString();
            
            //IApplicationContext cxt = ContextRegistry.GetContext();
            //IUserInfoService service = (IUserInfoService)cxt.GetObject("Service.UserInfo");
            //if (string.IsNullOrEmpty(User.Identity.Name))
            //{
            //    log.UserName = "匿名";
            //}
            //else
            //{
            //    var user = service.Get(Convert.ToInt32(User.Identity.Name));
            //    log.UserName = user.Name;
            //}
            //log.StackTrace = ex.StackTrace;
            //log.Solve = "false";
            //logger.Error(log);

            //Server.Transfer("~/Views/Shared/Error.htm");//导航到错误页
        }
    }
}